package com.yzd.crm.web.action;

import com.opensymphony.xwork2.ModelDriven;
import com.yzd.crm.domain.Customer;
import com.yzd.crm.domain.LinkMan;
import com.yzd.crm.domain.PageBean;
import com.yzd.crm.service.CustomerService;
import com.yzd.crm.service.LinkManService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/4/4 19:47
 * @Description:
 */
@Controller("linkManAction")
@Scope("prototype")
public class LinkManAction implements ModelDriven<LinkMan> {

    private LinkMan linkMan = new LinkMan();

    @Override
    public LinkMan getModel() {
        return linkMan;
    }

    @Autowired
    private CustomerService customerService;
    @Autowired
    private LinkManService linkManService;

    private Integer currPage = 1;
    private Integer pageSize = 2;


    private List<Customer> customers;
    public List<Customer> getCustomers() {
        return customers;
    }
    public String addUI(){
        customers = customerService.findAll();
        return "addUI";
    }

    public String save(){
        linkManService.save(linkMan);
        return "save";
    }

    private List<LinkMan> linkMans;
    public List<LinkMan> getLinkMans() {
        return linkMans;
    }
//    public String list(){
//        //linkMans = linkManService.findAll();
//        DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
//        if(linkMan != null && !StringUtils.isEmpty(linkMan.getLkmName())){
//            dc.add(Restrictions.like("lkmName", "%" + linkMan.getLkmName() + "%"));
//        }
//        linkMans = linkManService.findByDc(dc);
//        return "list";
//    }

    private PageBean<LinkMan> pb;
    public PageBean<LinkMan> getPb() {
        return pb;
    }
    /**
     * 分页
     */
    public String list(){
        DetachedCriteria dc = DetachedCriteria.forClass(LinkMan.class);
        if(linkMan != null && !StringUtils.isEmpty(linkMan.getLkmName())){
            dc.add(Restrictions.like("lkmName", "%" + linkMan.getLkmName() + "%"));
        }
        pb = linkManService.findByDc(dc, currPage, pageSize);
        return "pageList";
    }

    private LinkMan linkmanFind;

    public LinkMan getLinkmanFind() {
        return linkmanFind;
    }

    public String editUI(){
        customers = customerService.findAll();
        linkmanFind = linkManService.findById(linkMan.getLkmId());
        return "editUI";
    }

    public String update(){
        linkManService.update(linkMan);
        return "save";
    }

    public String delete(){
        linkManService.delete(linkMan);
        return "save";
    }
}
